package interfaces.subpanel.preprocesamiento.gui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import model.ClusteredGenericScenarie;

import org.apache.commons.collections.CollectionUtils;

import comparators.ClusteredGenericScenarieComparator;

public class ClusteredScenariesTable extends AbstractTableModel {

	private static final long serialVersionUID = 1L;

	private static final String SCENARIE_COLUMN_NAME = "Scenarie";

	private static final String CLUSTER_COLUMN_NAME = "Cluster";

	private String[] columnNames = { SCENARIE_COLUMN_NAME,
			CLUSTER_COLUMN_NAME };

	private List<ClusteredGenericScenarie> data = new ArrayList<ClusteredGenericScenarie>();

	private ClusteredGenericScenarieComparator genericScenarieComparator = new ClusteredGenericScenarieComparator();

	public int getColumnCount() {
		return this.columnNames.length;
	}

	public int getRowCount() {
		return this.data.size();
	}

	public String getColumnName(int col) {
		return this.columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		if (this.data.size() <= row) {
			System.out
					.println("ERROR: Out of Index while getting data from model in table.");
			return null;
		}
		ClusteredGenericScenarie scenarie = this.data.get(row);
		if (col == 0)
			return (String) scenarie.getScenarie();
		else
			return String.valueOf(scenarie.getCluster());
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass(int c) {
		return getValueAt(0, c).getClass();
	}

	public boolean isCellEditable(int row, int col) {
		if (col == 0) {
			return true;
		} else {
			return false;
		}
	}

	public void addItem(ClusteredGenericScenarie genericScenarie) {
		if (genericScenarie != null) {
			this.data.add(genericScenarie);
			Collections.sort(this.data, this.genericScenarieComparator);
			fireTableDataChanged();
		}
	}

	public void addAllGenericScenaries(List<ClusteredGenericScenarie> genericScenaries) {
		if (CollectionUtils.isNotEmpty(genericScenaries)) {
			this.data.addAll(genericScenaries);
			Collections.sort(this.data, this.genericScenarieComparator);
			fireTableDataChanged();
		}
	}

	public void setValueAt(Object value, int row, int col) {
		if (data.size() <= row) {
			System.out
					.println("ERROR: Out of Index while setting data into data model in table.");
			return;
		}
		if (col == 0) {
			data.get(row).setScenarie(((ClusteredGenericScenarie) value).getScenarie());
		} else {
			data.get(row).setCluster(((ClusteredGenericScenarie) value).getCluster());
		}
		fireTableCellUpdated(row, col);
	}

	public int dataSize() {
		return this.data.size();
	}

	public boolean isEmpty() {
		return CollectionUtils.isEmpty(this.data);
	}

	public boolean isNotEmpty() {
		return !this.isEmpty();
	}

	public ClusteredGenericScenarie getGenericScenarie(int index) {
		ClusteredGenericScenarie genericScenarie = null;
		if (this.data.size() > index)
			genericScenarie = this.data.get(index);
		return genericScenarie;
	}

	public void removeGenericScenarie(int index) {
		if (this.data.size() > index) {
			this.data.remove(index);
			fireTableDataChanged();
		}
	}

	public List<ClusteredGenericScenarie> removeIndexes(int[] genericScenariesIndexes) {

		List<ClusteredGenericScenarie> removedScenaries = new ArrayList<ClusteredGenericScenarie>();
		List<ClusteredGenericScenarie> notRemovedScenaries = new ArrayList<ClusteredGenericScenarie>();
		if (CollectionUtils.isNotEmpty(this.data)) {
			int i = 0, j = 0;
			while (i < genericScenariesIndexes.length) {
				int indexToRemove = genericScenariesIndexes[i];
				while (j < this.data.size()) {
					if (j == indexToRemove) {
						removedScenaries.add(this.data.get(indexToRemove));
						if (i < genericScenariesIndexes.length - 1)
							break;
					} else
						notRemovedScenaries.add(new ClusteredGenericScenarie(this.data
								.get(indexToRemove).getCluster(), this.data.get(
								indexToRemove).getScenarie()));
					j++;
				}
				i++;
			}
			this.data.removeAll(removedScenaries);
			fireTableDataChanged();
		}

		return removedScenaries;
	}

	public void removeAllGenericScenaries() {
		this.data.clear();
		fireTableDataChanged();
	}

	public List<ClusteredGenericScenarie> getAllGenericScenaries() {
		if (this.data == null)
			this.data = new ArrayList<ClusteredGenericScenarie>();
		return this.data;
	}

}
